﻿///-----------------------------------------------------------------------
/// <copyright file="PeriodsAllocationListViewViewModel.cs" company="FactSet Research Systems, Inc.">
///     Copyright © FactSet Research Systems, Inc.  All rights reserved 2012.
/// </copyright>
/// <author>Jonathan K. Barez</author>
///-----------------------------------------------------------------------
namespace HousholdISv2.ViewModel.Transaction
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Windows;
    using DataRepository;
    using MVVMCore;
    using MVVMCore.ViewModel.Details;
    using MVVMCore.ViewModel.Core;

    public class PeriodsAllocationListViewViewModel :
        ListViewViewModel<PeriodsAllocationListViewItemViewModel, PeriodAllocation>
    {
        #region Fields

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PeriodsAllocationListViewViewModel"/> class.
        /// </summary>
        public PeriodsAllocationListViewViewModel()
        {
            this.AutoCompleteText = new ObservableCollection<string>();
            HighlightedForeground = Foreground;
        }

        #endregion Constructors

        #region Events

        #endregion Events

        #region Properties

        public override bool IsReadOnly
        {
            get { return base.IsReadOnly; }
            set
            {
                if (base.IsReadOnly == value)
                {
                    return;
                }

                base.IsReadOnly = value;
                this.AutoCompleteText.Clear();
                if (value)
                {
                    return;
                }

                this.LoadACTextFromDb();
                this.RefreshACText();
            }
        }

        public ObservableCollection<string> AutoCompleteText { get; set; }

        public bool CanSave
        {
            get { return Items.All(item => item.CanBeSaved); }
        }

        public decimal Total
        {
            get { return GetValue(() => this.Total); }
            set { SetValue(() => this.Total, value); }
        }

        #endregion Properties

        #region Methods

        public PeriodsAllocationListViewItemViewModel Add(PeriodAllocation data)
        {
            var item = new PeriodsAllocationListViewItemViewModel(this, data);
            Add(item);
            return item;
        }

        public void AddRange(IEnumerable<PeriodAllocation> allocations)
        {
            allocations.ForEach(item => this.Add(item));
        }

        public void RefreshACText()
        {
            var items =
                this.AutoCompleteText.ToArray()
                    .Union(from item in Items
                           where item.IsSelected == false
                           select item.CommentText)
                    .Distinct()
                    .OrderBy(item => item)
                    .ToArray();
            this.AutoCompleteText.Clear();
            this.AutoCompleteText.AddRange(items);
        }

        public void UpdateTotal()
        {
            this.Total = this.Sum(item => item.Amount);
        }

        public override PeriodsAllocationListViewItemViewModel Remove(PeriodsAllocationListViewItemViewModel item)
        {
            var removed = base.Remove(item);
            this.UpdateTotal();
            return removed;
        }

        private void LoadACTextFromDb()
        {
            this.AutoCompleteText.Clear();
            using (var dc = Context.GetContextInstance())
            {
                this.AutoCompleteText.AddRange(
                    (from r in dc.PeriodAllocations
                     orderby r.Comment
                     select r.Comment).Distinct());
            }
        }

        #endregion Methods
    }
}